package org.springblade.modules.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.admin.entity.Elevator;
import org.springblade.modules.admin.entity.ElevatorAudit;
import org.springblade.modules.admin.enums.ElevatorAuditEnum;
import org.springblade.modules.admin.mapper.ElevatorAuditMapper;
import org.springblade.modules.admin.service.ElevatorAuditService;
import org.springblade.modules.admin.service.ElevatorService;
import org.springblade.modules.admin.utils.ElevatorAuditUtil;
import org.springblade.modules.admin.utils.VCodeResult;
import org.springblade.modules.admin.vo.feishu.FeiShuAnnualReviewDateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ElevatorAuditServiceImpl extends BaseServiceImpl<ElevatorAuditMapper, ElevatorAudit> implements ElevatorAuditService {

	@Autowired
	private ElevatorService elevatorService;
//	@Autowired
//	DelayedSend delayedSend;
	@Autowired
	private ElevatorAuditUtil elevatorAuditUtil;

	private static long time = 8*60*60*1000;

	@Override
	public Boolean updateElevatorAudit(ElevatorAudit entity) {

		Elevator elevator = elevatorService.getById(entity.getElevatorId());
		elevator.setElevatorAudit(entity.getAuditTime());
		elevatorService.updateById(elevator);

		ElevatorAudit elevatorAudit = this.getOne(Wrappers.<ElevatorAudit>lambdaQuery().eq(ElevatorAudit::getElevatorId,entity.getElevatorId()));
		if(ObjectUtil.isNotEmpty(elevatorAudit)){
			entity.setId(elevatorAudit.getId());
		}
		this.saveOrUpdate(entity);

		// 处理发送电梯检查消息队列
		String privateNum = elevatorService.getObj(Wrappers.<Elevator>lambdaQuery().select(Elevator::getPrivateNum).eq(Elevator::getId,entity.getElevatorId()),Object::toString);
		JSONObject msgObject = null;
		if(ObjectUtil.isNotEmpty(entity.getRemindAuditTime())){
			msgObject = new JSONObject(5);
			msgObject.put("tenantId", AuthUtil.getTenantId());
			msgObject.put("privateNum",privateNum);
			msgObject.put("checkType", ElevatorAuditEnum.AUDIT.getCode());
			msgObject.put("sendTime",entity.getRemindAuditTime());
			long sendTime = DateUtil.parseDate(entity.getRemindAuditTime()).getTime()+time;
//			if(ObjectUtil.isNotEmpty(elevatorAudit)){
//				if(!elevatorAudit.getRemindAuditTime().equals(entity.getRemindAuditTime())){
//					delayedSend.sendDelayed(sendTime,JSONObject.toJSONString(msgObject));
//				}
//			}else {
//				delayedSend.sendDelayed(sendTime,JSONObject.toJSONString(msgObject));
//			}
		}
		if(ObjectUtil.isNotEmpty(entity.getRemindLoadTime())){
			msgObject = new JSONObject(5);
			msgObject.put("tenantId", AuthUtil.getTenantId());
			msgObject.put("privateNum",privateNum);
			msgObject.put("checkType", ElevatorAuditEnum.LOAD.getCode());
			msgObject.put("sendTime",entity.getRemindLoadTime());
			long sendTime = DateUtil.parseDate(entity.getRemindLoadTime()).getTime()+time;
//			if(ObjectUtil.isNotEmpty(elevatorAudit)){
//				if(!elevatorAudit.getRemindLoadTime().equals(entity.getRemindLoadTime())){
//					delayedSend.sendDelayed(sendTime,JSONObject.toJSONString(msgObject));
//				}
//			}else {
//				delayedSend.sendDelayed(sendTime,JSONObject.toJSONString(msgObject));
//			}
		}
		if(ObjectUtil.isNotEmpty(entity.getRemindSpeedTime())){
			msgObject = new JSONObject(5);
			msgObject.put("tenantId", AuthUtil.getTenantId());
			msgObject.put("privateNum",privateNum);
			msgObject.put("checkType", ElevatorAuditEnum.SPEED.getCode());
			msgObject.put("sendTime",entity.getRemindSpeedTime());
			long sendTime = DateUtil.parseDate(entity.getRemindSpeedTime()).getTime()+time;
//			if(ObjectUtil.isNotEmpty(elevatorAudit)){
//				if(!elevatorAudit.getRemindSpeedTime().equals(entity.getRemindSpeedTime())){
//					delayedSend.sendDelayed(sendTime,JSONObject.toJSONString(msgObject));
//				}
//			}else {
//				delayedSend.sendDelayed(sendTime,JSONObject.toJSONString(msgObject));
//			}
		}

		return Boolean.TRUE;
	}

	@Override
	public int countBuildingElevatorAudit(Long buildingId, String checkTime, Integer checkType) {
		return baseMapper.countBuildingElevatorAudit(buildingId, checkTime, checkType);
	}

	@Override
	public String getElevatorAuditTimeByPrivateNum(String privateNum) {
		String auditTime = elevatorAuditUtil.vCodeByPrivateNum(privateNum);
		return auditTime.equals("false") ? null : auditTime;
	}

	@Override
	public String batchUpdateElevatorAuditTime(){
		// 获取所有设备代码
		List<String> privateNums = elevatorService.list(Wrappers.<Elevator>lambdaQuery().eq(Elevator :: getIsDeleted, 0).isNotNull(Elevator::getPrivateNum))
			.stream().map(t -> t.getPrivateNum()).collect(Collectors.toList());
		VCodeResult result = elevatorAuditUtil.vCodeByPrivateNumList(privateNums);
		List<Map<String, String>> successResultList = result.getSuccessResultList();
		for (Map<String, String> resultMap : successResultList) {
			Elevator elevator = elevatorService.getOne(Wrappers.<Elevator>lambdaQuery().eq(Elevator :: getPrivateNum, resultMap.get("privateNum")), false);
			if (elevator == null) {
				continue;
			}
			elevator.setElevatorAudit(resultMap.get("yearCheckTime"));
			if (elevatorService.updateById(elevator)) {
				ElevatorAudit elevatorAudit = new ElevatorAudit();
				elevatorAudit.setElevatorId(elevator.getId());
				elevatorAudit.setAuditTime(resultMap.get("yearCheckTime"));
				this.updateElevatorAudit(elevatorAudit);
			}
		}

		StringBuilder msg = new StringBuilder("total: ").append(result.getTotal())
			.append(" ,success: ").append(result.getSuccessCount())
			.append(" ,error: ").append(result.getFailCount());
		return msg.toString();
	}

	@Override
	public List<FeiShuAnnualReviewDateVO> getAnnualReviewDateByDeviceCodes(List<String> deviceCodes) {
		return baseMapper.getAnnualReviewDateByDeviceCodes(deviceCodes);
	}
}

